/**
 * 
 */
package com.zz.datareport.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.zz.datareport.schema.Project;
import com.zz.datareport.service.ProjectService;
import com.zz.datareport.service.impl.ProjectServiceImpl;
import com.zz.datareport.test.DataObjectToJavaBean;
import com.zz.datareport.util.Constants;
import com.zz.datareport.util.GeneralUtil;
import com.zz.datareport.util.GenerateZipUtil;
import com.zz.datareport.util.JaxbObjectAndXmlUtil;

/**
 * @author Wangys
 * @date 2018年10月7日
 */
public class ProjectController {
	
	private  ProjectService projectService = new ProjectServiceImpl();
	
	public void findProjectById(String id){
		Project project = projectService.findProjectById(id);
		String xmlStr = JaxbObjectAndXmlUtil.object2Xml(project);//构造报文 XML 格式的字符串
		String projectXmlStr = GeneralUtil.addXmlHead(xmlStr, Project.class.getSimpleName().toLowerCase());
		//格式如：project_69E572F1-2FCD-4D1B-8C86-B7621002E90B.xml
		String xmlFileName =Project.class.getSimpleName().toLowerCase()+"_"+UUID.randomUUID();
		try {
			File file = new File(GeneralUtil.readConfigurationByProperties("constitute_file_path")+xmlFileName+".xml");
			//在指定位置生成xml
			GenerateZipUtil.outPutFile(projectXmlStr, file);
			//打包成zip文件
			List<File> files = new ArrayList<>();
			files.add(file);
			OutputStream os = new BufferedOutputStream(new FileOutputStream(GeneralUtil.readConfigurationByProperties("zip_path")+Project.class.getSimpleName().toLowerCase()+".zip"));
			GenerateZipUtil.generateZip(os, files);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
